package com.website.travle.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.website.travle.mapper.IUserDAOImpl;
import com.website.travle.pojo.User;
import com.website.travle.utils.ApiResp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/api/user")
public class UserController extends HttpServlet {

    private static IUserDAOImpl userDAO = new IUserDAOImpl();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        switch (operation) {
            case "add":
                try {
                    add(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "delete":
                try {
                    delete(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "update":
                try {
                    update(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "query":
                try {
                    query(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "login":
                try {
                    login(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;

            default:
                error(req, resp);
                break;

        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int login = userDAO.login(username, password);
        if (login == 1) {
            // 创建Gson对象
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            // 将Person对象转换为JSON字符串
            String json = gson.toJson(new ApiResp<>().successfully());
            // 设置响应内容类型为JSON
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        } else {
            // 创建Gson对象
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            // 将Person对象转换为JSON字符串
            String json = gson.toJson(new ApiResp<>().failed("登录失败。"));
            // 设置响应内容类型为JSON
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        }
    }


    private void error(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("operation error!!!");
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        List<User> users = userDAO.pageByLikeName(Integer.valueOf(page), Integer.valueOf(limit), name);
        // 创建Gson对象
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        // 将Person对象转换为JSON字符串
        String json = gson.toJson(new ApiResp<>(0, "success", userDAO.getTotal(), users));
        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        User user = new User();
        user.setId(Integer.valueOf(req.getParameter("userId")));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setPhone(req.getParameter("phone"));
        user.setNickname(req.getParameter("nickname"));
        user.setIdentity(Integer.valueOf(req.getParameter("identity")));
        System.out.println(user);
        int update = userDAO.update(user);
        // 创建Gson对象
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = null;
        if (update == 1) {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().successfully());
        } else {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().failed("更新失败。"));
        }

        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        int delete = userDAO.delete(Integer.valueOf(id));
        // 创建Gson对象
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = null;
        if (delete == 1) {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().successfully());
        } else {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().failed("删除失败。"));
        }
        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setPhone(req.getParameter("phone"));
        user.setNickname(req.getParameter("nickname"));
        user.setIdentity(Integer.valueOf(req.getParameter("identity")));
        System.out.println(user);
        int add = userDAO.add(user);
        // 创建Gson对象
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = null;
        if (add == 1) {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().successfully());
        } else {
            // 将Person对象转换为JSON字符串
            json = gson.toJson(new ApiResp<>().failed("添加失败,用户已存在。"));
        }

        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }


}
